using System;
using NUnit.Framework;
using SmartPeople.Framework.SecuritySpace.Tests;

namespace SmartPeople.Framework.SecuritySpace.Provider.Tests
{
    [TestFixture]
    public class SecuredTypesTests
    {
        [Test, Description("Create a type,but security space not updated (db)")]
        public void CreateATypeWithoutUpdatingSecuritySpace()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();

            string typeName = "WordFile";
            ISecuritySecuredType securedType1 = securitySpace.SecuredTypes.CreateType(typeName);

            Assert.AreEqual(null, securitySpace.SecuredTypes.GetSecuredType("WorkFile"));

            securitySpace.Clear();
        }

        [Test, Description("Create a type,but security space not updated (db)")]
        public void CreateATypeUpdatingSecuritySpace()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();

            string typeName = "WordFile";
            ISecuritySecuredType securedType1 = securitySpace.SecuredTypes.CreateType(typeName);


            securitySpace.SecuredTypes.UpdateSecuredType(securedType1);
            Assert.IsTrue(securitySpace.SecuredTypes.SecuredTypeExists("WordFile"));


            Assert.AreEqual("WordFile", securitySpace.SecuredTypes.GetSecuredType("WordFile").Name);

            securitySpace.Clear();
        }


        //SecType_001
        [Test]
        public void CreateDeleteSecuredTypesTests()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();

            string typeName = "WordFile";
            ISecuritySecuredType securedType1 = securitySpace.SecuredTypes.CreateType(typeName);
            Assert.IsNotNull(securedType1);
            Assert.AreEqual(securedType1.Name, securitySpace.SecuredTypes.GetSecuredType(securedType1.Name).Name);
            Assert.IsTrue(securitySpace.SecuredTypes.SecuredTypeExists(typeName));
            securitySpace.SecuredTypes.DeleteSecuredType(typeName, true);
            Assert.IsFalse(securitySpace.SecuredTypes.SecuredTypeExists(typeName));

            securitySpace.Clear();
        }


        //SecType_002
        [Test]
        public void ActionsStatesTests()
        {
            ISecuritySpace securitySpace = TestsHelper.GetSecuritySpace();
            securitySpace.Clear();

            ISecuritySecuredType securedFolder = securitySpace.SecuredTypes.CreateType("folderx1");
            securedFolder.Actions.CreateAction(TestActions.List);
            securedFolder.Actions.CreateAction(TestActions.Read);
            securedFolder.Actions.CreateAction(TestActions.Create);
            securedFolder.Actions.CreateAction(TestActions.Modify);
            securedFolder.Actions.CreateAction(TestActions.Delete);
            securedFolder.States.CreateState(TestStates.ReadOnly);
            securitySpace.SecuredTypes.UpdateSecuredType(securedFolder);
            Assert.AreEqual(5, securedFolder.Actions.Count);
            Assert.AreEqual(1, securedFolder.States.Count);

            ISecuritySecuredType inheritedFolder =securitySpace.SecuredTypes.CreateType("ContactsFolder", securedFolder);
            inheritedFolder.Actions.CreateAction("Call");
            inheritedFolder.States.CreateState("Closed");
            //the security folder must be updated

            securitySpace.SecuredTypes.UpdateSecuredType(inheritedFolder);
            Assert.AreEqual(6, inheritedFolder.Actions.Count);
            Assert.AreEqual(2, inheritedFolder.States.Count);

            Assert.IsNotNull(inheritedFolder.Actions[TestActions.Create]);
            Assert.IsNotNull(inheritedFolder.States[TestStates.ReadOnly]);

            securitySpace.SecuredTypes.DeleteSecuredType("folderx1", true);
            Assert.IsFalse(securitySpace.SecuredTypes.SecuredTypeExists("folderx1"));

            securitySpace.Clear();
        }


        //TODO:Enchance this test
        [Test,Description("Create same types for two different applications")]
        public void SampleTypesforDifferentApplication()
        {
            string typeName = "WordFile";
            
            ISecuritySpace securitySpaceTA = TestsHelper.GetSecuritySpace("TA");
            ISecuritySecuredType securedTypeA = securitySpaceTA.SecuredTypes.CreateType(typeName);

            securitySpaceTA.SecuredTypes.UpdateSecuredType(securedTypeA);

            Guid securedTypeAGID = securedTypeA.GID;
            //Assert.AreEqual(1, securitySpaceTA.SecuredTypes.GetAllSecuredTypes().Length);
            Assert.AreEqual(typeName, securitySpaceTA.SecuredTypes.GetSecuredType(typeName).Name);
            //3 + 2 created because of default type
            Assert.AreEqual(3, securitySpaceTA.SecuredTypes.Count);

            ISecuritySpace securitySpaceHR = TestsHelper.GetSecuritySpace("HR");
            ISecuritySecuredType securedTypeB = securitySpaceHR.SecuredTypes.CreateType(typeName);
            securitySpaceHR.SecuredTypes.UpdateSecuredType(securedTypeB);
            Guid securedTypeBGID = securedTypeB.GID;
            //3 because of default type
            Assert.AreEqual(3, securitySpaceHR.SecuredTypes.Count);

            

            securedTypeA = securitySpaceTA.SecuredTypes.GetSecuredType(typeName);
            Assert.AreEqual(typeName,securedTypeA.Name);
            Assert.AreEqual(securedTypeAGID, securedTypeA.GID);
            securedTypeA.Name += "AA";
            securitySpaceTA.SecuredTypes.UpdateSecuredType(securedTypeA);

            securedTypeA = securitySpaceTA.SecuredTypes.GetSecuredType(typeName + "AA");
            Assert.AreEqual(securedTypeAGID, securedTypeA.GID);

            securedTypeB = securitySpaceHR.SecuredTypes.GetSecuredType(typeName);
            Assert.AreEqual(typeName, securedTypeB.Name);
            Assert.AreEqual(securedTypeBGID, securedTypeB.GID);
            securedTypeB.Name += "BB";
            securitySpaceHR.SecuredTypes.UpdateSecuredType(securedTypeB);

            securedTypeB = securitySpaceHR.SecuredTypes.GetSecuredType(typeName + "BB");
            Assert.AreEqual(securedTypeBGID, securedTypeB.GID);


            //securitySpaceTA.SecuredTypes.DeleteSecuredType();

            securitySpaceTA.SecuredTypes.Clear();
            //2 because of default type
            Assert.AreEqual(2, securitySpaceTA.SecuredTypes.GetAllSecuredTypes().Length);
            //1 because of default type & 2 remaining
            Assert.AreEqual(3, securitySpaceHR.SecuredTypes.GetAllSecuredTypes().Length);

            securitySpaceHR.SecuredTypes.Clear();
            //2 because of default type
            Assert.AreEqual(2, securitySpaceTA.SecuredTypes.GetAllSecuredTypes().Length);
            //1 because of default type
            Assert.AreEqual(2, securitySpaceHR.SecuredTypes.GetAllSecuredTypes().Length);

            securitySpaceHR.Clear();
            securitySpaceTA.Clear();
            
        }
    }
}